package com.kscrm.web.controller.web;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.kscrm.common.core.domain.R;
import com.kscrm.common.utils.SecurityUtils;
import com.kscrm.web.domain.*;
import com.kscrm.web.dto.*;
import com.kscrm.web.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kscrm.common.annotation.Log;
import com.kscrm.common.core.controller.BaseController;
import com.kscrm.common.core.domain.AjaxResult;
import com.kscrm.common.enums.BusinessType;
import com.kscrm.common.core.page.TableDataInfo;

/**
 * 合作项目Controller
 *
 * @author Dhq
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/web/project")
public class ProjectController extends BaseController {
    @Autowired
    private IProjectService projectService;

    @Autowired
    private IClientExpertService clientExpertService;

    @Autowired
    private IProjectClientService projectClientService;
    @Autowired
    private IProjectExpertService projectExpertService;

    @Autowired
    private IProjectChannelService projectChannelService;

    /**
     * 查询合作项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 查询合作项目列表
     */
    @GetMapping("/menuList")
    public R<List<Project>> menuList() {
        Long channelId = SecurityUtils.getChannelId();
        return R.ok(projectService.selectProjectList(new Project().setChannelId(channelId)));
    }


    /**
     * 项目专家
     *
     * @return
     */
    @PutMapping(value = "editExpert")
    public AjaxResult editExpert(@RequestBody @Valid AddProjectExpertDto dto) {
        Project project = projectService.selectProjectById(dto.getProjectId());
        if (Objects.isNull(project)) {
            return error("项目不存在");
        }
        projectExpertService.deleteProjectExpertByProjectId(dto.getProjectId());
        for (Long expertId : dto.getExpertIdList()) {
            projectExpertService.insertProjectExpert(new ProjectExpert().setProjectId(dto.getProjectId()).setExpertId(expertId));
            // 新增客户与专家关系绑定
            for (Client client : project.getClientList()) {
                clientExpertService.insertClientExpert(
                        new ClientExpert()
                               .setClientId(client.getId())
                               .setExpertId(expertId));
            }
        }
        return success();
    }

    /**
     * 项目客户
     *
     * @return
     */
    @PutMapping(value = "editClient")
    public AjaxResult editClient(@RequestBody @Valid AddProjectClientDto dto) {
        Project project = projectService.selectProjectById(dto.getProjectId());
        if (Objects.isNull(project)) {
            return error("项目不存在");
        }
        projectClientService.deleteProjectClientByProjectId(dto.getProjectId());
        for (Long clientId : dto.getClientIdList()) {
            // 新增客户
            projectClientService.insertProjectClient(new ProjectClient().setClientId(clientId).setProjectId(dto.getProjectId()));
            // 新增客户与专家关系绑定
            for (Expert expert : project.getExpertList()) {
                clientExpertService.insertClientExpert(
                        new ClientExpert()
                                .setClientId(clientId)
                                .setExpertId(expert.getId()));
            }
        }
        return success();
    }

    /**
     * 项目渠道
     *
     * @return
     */
    @PutMapping(value = "editChannel")
    public AjaxResult editChannel(@RequestBody @Valid AddProjectChannelDto dto) {
        projectChannelService.deleteProjectChannelByProjectId(dto.getProjectId());
        for (Long channelId : dto.getChannelIdList()) {
            // 新增渠道
            projectChannelService.insertProjectChannel(new ProjectChannel().setChannelId(channelId).setProjectId(dto.getProjectId()));
        }
        return success();
    }

    /**
     * 获取合作项目详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(projectService.selectProjectById(id));
    }

    /**
     * 新增合作项目
     */
    @Log(title = "合作项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Project project) {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改合作项目
     */
    @Log(title = "合作项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project) {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除合作项目
     */
    @Log(title = "合作项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(projectService.deleteProjectByIds(ids));
    }
}
